﻿using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Drawing;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using PixelFormat = System.Drawing.Imaging.PixelFormat;

namespace MazeRunner.Graphics
{
	public static class OpenGLSetup
	{
		public static List<int> textures;
		public static void InitializeOpenGL(int width, int height)
		{
			GL.Viewport(0, 0, width, height);
			GL.ClearColor(0, 0, 0, 0);
			float[] mat_specular = { 1.0f, 1.0f, 1.0f, 1.0f };
			float[] mat_shininess = { 50.0f };
			float[] light_position = { 1.0f, 1.0f, 1.0f, 0.0f };
			float[] light_ambient = { 0.25f, 0.25f, 0.25f, 1.0f };

			GL.ClearColor(0.0f, 0.0f, 0.0f, 0.0f);
			GL.ShadeModel(ShadingModel.Smooth);

			GL.Material(MaterialFace.Front, MaterialParameter.Specular, mat_specular);
			GL.Material(MaterialFace.Front, MaterialParameter.Shininess, mat_shininess);
			GL.Light(LightName.Light0, LightParameter.Position, light_position);
			GL.Light(LightName.Light0, LightParameter.Ambient, light_ambient);
			GL.Light(LightName.Light0, LightParameter.Diffuse, mat_specular);
			GL.Enable(EnableCap.Lighting);
			GL.Enable(EnableCap.Light0);
			GL.Enable(EnableCap.DepthTest);
			GL.Enable(EnableCap.ColorMaterial);
			GL.Enable(EnableCap.Texture2D);
			GL.Enable(EnableCap.CullFace);
			Matrix4d projection = Matrix4d.CreatePerspectiveFieldOfView((float) (Math.PI/4), (float) width/height, 1.0, 300.0);
			GL.LoadMatrix(ref projection);

			textures = new List<int>();

			int[] id = new int[3];
			GL.GenTextures(3, id);
			GL.BindTexture(TextureTarget.Texture2D, id[0]);

			Bitmap bmp = new Bitmap(Path.Combine(Environment.CurrentDirectory, "wall.png"));
			BitmapData bmp_data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly,
			                                   PixelFormat.Format32bppArgb);



			GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, bmp_data.Width, bmp_data.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, bmp_data.Scan0);

			bmp.UnlockBits(bmp_data);

			GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
			GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);

			textures.Add(id[0]);
			bmp.Dispose();

			GL.BindTexture(TextureTarget.Texture2D, id[1]);

			bmp = new Bitmap(Path.Combine(Environment.CurrentDirectory, "ceiling.png"));
			bmp_data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly,
											   PixelFormat.Format32bppArgb);

			GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, bmp_data.Width, bmp_data.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, bmp_data.Scan0);

			bmp.UnlockBits(bmp_data);

			GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
			GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);

			textures.Add(id[1]);
			bmp.Dispose();

			GL.BindTexture(TextureTarget.Texture2D, id[2]);

			bmp = new Bitmap(Path.Combine(Environment.CurrentDirectory, "floor.png"));
			bmp_data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly,
											   PixelFormat.Format32bppArgb);

			GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, bmp_data.Width, bmp_data.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, bmp_data.Scan0);

			bmp.UnlockBits(bmp_data);

			GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
			GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);

			textures.Add(id[2]);
			bmp.Dispose();
		}
	}
}
